package com.fxust.appmain;

import com.fxust.common.CommonDefine;
import com.fxust.dialog.*;
import com.fxust.utils.FileUtils;
import com.fxust.utils.UUIDUtil;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author johnking
 * @create  2017-12-17 下午9:23
 **/
public class BatchMainFrame extends JFrame {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = -1080840153638620668L;

    private static final Logger logger = LoggerFactory.getLogger(BatchMainFrame.class);

    /** 主frame对象 */
    private static JFrame frame = null;
    /**
     * 界面控件声明
     */
    private JPanel contentPane = null;
    private JButton configButton = null;
    private JButton versionButton = null;
    private JButton downLoadPicButton = null;
    private JButton uploadPicButton = null;
    private JButton textFileButton = null;
    private BaseDialog dialog;
    // 全局唯一id
    private String uuid = UUIDUtil.getRandomUUID();

    /**
     * 构造实例
     */
    public BatchMainFrame() {
        setTitle("批量数据处理工具");
        setBounds(100, 100, 460, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 主体Panel
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);
        {
            configButton = new JButton("系统配置");
            configButton.setBounds(353, 10, 93, 23);
            contentPane.add(configButton);
        }
        JPanel funcPanel = new JPanel();
        funcPanel.setLayout(null);
        funcPanel.setBorder(BorderFactory.createTitledBorder("功能选项"));
        funcPanel.setBackground(Color.WHITE);
        funcPanel.setBounds(10, 40, 436, 223);
        contentPane.add(funcPanel);
        {
            {
                downLoadPicButton = new JButton("图片下载");
                downLoadPicButton.setBounds(10, 30, 90, 23);
                funcPanel.add(downLoadPicButton);
            }
            {
                uploadPicButton = new JButton("图片上传");
                uploadPicButton.setBounds(120, 30, 90, 23);
                funcPanel.add(uploadPicButton);
            }
            {
                textFileButton = new JButton("文本入库");
                textFileButton.setBounds(10, 70, 90, 23);
                funcPanel.add(textFileButton);
            }
            {
                versionButton = new JButton("版本信息");
                versionButton.setBounds(10, 180, 90, 23);
                funcPanel.add(versionButton);
            }
        }
        addListener();
    }

    /**
     * 监听事件初始化
     * @author fgq 2017年11月25日 下午4:18:38
     */
    private void addListener() {
        configButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                openDatabaseConfigDialog();
            }
        });
        versionButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                openVersionInfoDialog();
            }
        });
        downLoadPicButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                openPicDownloadDialog();
            }
        });
        uploadPicButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                openPicUploadDialog();
            }
        });
        textFileButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                openTextFileDialog();
            }
        });

    }

    /**
     * 系统配置
     * @author fgq 2017年11月25日 下午4:18:55
     */
    private void openDatabaseConfigDialog() {
        dialog = new DatabaseConfigDialog(this, "系统配置");
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    /**
     * 版本信息
     * @author fgq 2017年11月25日 下午4:19:04
     */
    private void openVersionInfoDialog() {
        dialog = new VersionConfigDialog(this, "版本信息");
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    /**
     * 图片下载
     * @author fgq 2017年11月25日 下午4:19:12
     */
    private void openPicDownloadDialog() {
        dialog = new DownloadPicDialog(this, "图片下载", uuid);
        CommonDefine.asyncTrainMap.put(uuid, dialog);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    /**
     * 图片上传
     * @author fgq 2017年11月25日 下午4:19:21
     */
    private void openPicUploadDialog() {
        dialog = new UploadPicDialog(this, "图片上传", uuid);
        CommonDefine.asyncTrainMap.put(uuid, dialog);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    /**
     * 文本处理
     * @author fgq 2017年11月25日 下午4:19:30
     */
    private void openTextFileDialog() {
        dialog = new DealTextFileDialog(this, "文本处理", uuid);
        CommonDefine.asyncTrainMap.put(uuid, dialog);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    /**
     * 主函数
     * @param args
     */
    public static void main(String[] args) {

        // 设置界面的字体:不同机子上, 默认字体不同, 统一设置字体、看起来界面统一
        System.setProperty("swing.plaf.metal.controlFont", "宋体");

        // 打印日志
        PropertyConfigurator.configure(FileUtils.getLogConfigPath());

        // 主线程启动、防止死锁
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    frame = new BatchMainFrame();
                    frame.setVisible(true);
                    frame.addWindowListener(new WindowAdapter() {
                        // 窗口关闭事件、用以系统崩溃的问题排查
                        public void windowClosing(WindowEvent e) {
                            logger.info("窗口被正常关闭");
                        }
                    });
                } catch (Exception e) {
                    logger.error("启动主界面报错: ", e);
                }
            }
        });
    }

}
